package com.wxm.servlet;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.xm.dao.BannerDaoImpl;
import com.xm.dao.ImageDaoImpl;
import com.xm.dao.LianXiDaoImpl;
import com.xm.dao.ProductDaoImpl;
import com.xm.dao.ProtypeDaoImpl;
import com.xm.entity.Product;
import com.xm.entity.Banners;
import com.xm.entity.Images;
import com.xm.entity.LianXi;
import com.xm.entity.producttype;

/**
 * 处理产品相关的请求
 */
@WebServlet("/FurnServlet")
public class FurnServlet extends BaseServlet {
	/**
	 * 全查 地址: http://localhost:8080/javaweb-Furniture/FurnServlet?method=toProduct
	 * toProduct所有精选查询方法、轮播图方法
	 * 
	 * @param req
	 * @param resp
	 * @throws ServletException
	 * @throws IOException
	 */
	public void toProduct(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// 查询推荐的前8个数据
		ProductDaoImpl ProDao = new ProductDaoImpl();
		ArrayList<Product> allList = ProDao.getAllList();

		// 产品类型列表
		ProtypeDaoImpl Protype = new ProtypeDaoImpl();
		ArrayList<producttype> allProtype = Protype.getAllProtype();
		// 单个联系信息
		LianXiDaoImpl dao3 = new LianXiDaoImpl();
		LianXi com2 = dao3.getCom().get(0);

		HttpSession session = req.getSession();
		session.setAttribute("com2", com2);

		// 存储在作用域
		req.setAttribute("selected", allList);// 推荐的前8个数据
		req.setAttribute("allProtype", allProtype);// 产品列表

		// 跳转页面(请求转发)
		req.getRequestDispatcher("productlist.jsp").forward(req, resp);
	}

	/**
	 * 根据产品名模糊查方法
	 * 
	 * @param req
	 * @param resp
	 * @throws ServletException
	 * @throws IOException
	 */
	public void ToPname(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String pname = req.getParameter("cha");
		if (pname != null && !pname.equals("")) {
			// 调用模糊查dao层方法
			ProductDaoImpl pnameDao = new ProductDaoImpl();
			ArrayList<Product> nameList = pnameDao.getName(pname);
			// 存储在作用域
			req.setAttribute("selected", nameList);
		} else {
			System.out.println("你查询的商品不存在");
		}
		// 产品类型列表
		ProtypeDaoImpl Protype = new ProtypeDaoImpl();
		ArrayList<producttype> allProtype = Protype.getAllProtype();
		// 存储在作用域
		req.setAttribute("allProtype", allProtype);// 产品列表

		// 跳转页面(请求转发)
		req.getRequestDispatcher("productlist.jsp").forward(req, resp);
	}

	/**
	 * 搜素产品，并跳转到产品详情
	 * 
	 * @param req
	 * @param resp
	 * @throws ServletException
	 * @throws IOException
	 */
	public void getNameList(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// 接收客户端的参数(数据类型的字符串)
		String proId = req.getParameter("id");
		// 讲字符串转换，并判断不为空
		if (proId != null && !proId.equals("")) {
			int id2 = Integer.valueOf(proId);
			BannerDaoImpl mohu = new BannerDaoImpl();// 调用dao层模糊查
//			Product product = mohu.getName(id2);
		}
		// 根据输入产品名查询产品信息

		// 跳转页面(请求转发)
		req.getRequestDispatcher("productlist.jsp").forward(req, resp);
	}

	/**
	 * 根据产品类id获取产品列表 ,当点击超链接时调用
	 * 
	 * @param req
	 * @param resp
	 * @throws ServletException
	 * @throws IOException
	 */
	public void getProByIype(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// 接收客户端的参数(数据类型的字符串)
		String protype = req.getParameter("protype");
		// 讲字符串转换，并判断不为空
		int typProInt = 0;
		if (protype != null && !protype.equals("")) {
			typProInt = Integer.valueOf(protype);
		}
		// 调用dao方法
		// 根据id查询,产品列表
		ProductDaoImpl protypes = new ProductDaoImpl();
		ArrayList<Product> selected = protypes.getListByProType(typProInt);
		// 产品类型列表
		ProtypeDaoImpl Protype = new ProtypeDaoImpl();
		ArrayList<producttype> allProtype = Protype.getAllProtype();

		// 存储在作用域
		req.setAttribute("selected", selected);// 根据产品列表id查询
		req.setAttribute("allProtype", allProtype);// 产品列表
		req.setAttribute("protype", protype);

		// 跳转页面(请求转发)
		req.getRequestDispatcher("productlist.jsp").forward(req, resp);
	}

	/**
	 * 单击产品图片查询详情页方法
	 * 
	 * @param req
	 * @param resp
	 * @throws ServletException
	 * @throws IOException
	 */
	public void getDetails(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

		// 接收客户端的参数(数据类型的字符串)
		String proId = req.getParameter("id");
		// 讲字符串转换，并判断不为空
		int ProIntId = 0;
		if (proId != null && !proId.equals("")) {
			ProIntId = Integer.valueOf(proId);
		}
		// 产品各方位轮播图
		ImageDaoImpl imgDao = new ImageDaoImpl();
		ArrayList<Images> imgListBypId = imgDao.getImgListBypId(ProIntId);

		// 单击图片查询产品详细信息dao方法
		ProductDaoImpl proDan = new ProductDaoImpl();
		ArrayList<Product> details = proDan.getChaDange(ProIntId);

		// 产品类型列表
		ProtypeDaoImpl Protype = new ProtypeDaoImpl();
		ArrayList<producttype> allProtype = Protype.getAllProtype();

		// 单个联系信息
		LianXiDaoImpl dao3 = new LianXiDaoImpl();
		LianXi com2 = dao3.getCom().get(0);

		HttpSession session = req.getSession();
		session.setAttribute("com2", com2);

		// 存储在作用域
		req.setAttribute("imgListBypId", imgListBypId);// 产品xx系列轮播图
		req.setAttribute("details", details);// 产品详情
		req.setAttribute("allProtype", allProtype);// 产品列表

		// 跳转页面(请求转发)
		req.getRequestDispatcher("productdetails.jsp").forward(req, resp);
	}

}
